<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@include file="/static/common/taglib.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>工序管理</title>
    <%@include file="../../public/commonJs.jsp" %>
    <%@include file="../../public/commonCss.jsp" %>
</head>
<body ms-controller="main" class="ms-controller">
<div id="wrapper">
    <div class="boxmain whiteBox lrbox">
        <div class="lrboxleft borderR" style="width:22%">
            <div class="from-header borderB ">
                <select ms-duplex="trainCode" onchange="selectChange(this)" onmouseover="selectClick(this)"
                        style="display: inline-block" id="trainCode">
                    <option value="">--选择车型--</option>
                    <option ms-attr-value="el.itemValue" ms-repeat="trainTypes">{{el.itemText}}</option>
                </select>
                <span class="classifyEdit fr">
					<button class="layui-btn layui-btn-primary layui-btn-small" ms-on-click="move(1)">上移</button>
					<button class="layui-btn layui-btn-primary layui-btn-small" ms-on-click="move(2)">下移</button>
					<button class="layui-btn layui-btn-danger layui-btn-small" ms-on-click="deleteSelectNode()"
                            style="padding:0 10px">删除</button>
				</span>
            </div>
            <div class="from-body withheader contentbox8">
                <ul id="treeDemo" class="ztree" style="width:100%;height: 100%;overflow: auto;"></ul>
            </div>
        </div>
        <div class="lrboxmain borderL" style="margin-left:22%">
            <div class="from-header borderB contentbox">
                <div>
                    <span class="font14"> 所属分类:</span><a id="category"><span class="fontOrange2"> {{title}}</span></a>
                    <button class="layui-btn layui-btn-normal layui-btn-small fr" ms-on-click="addWork()">新增工序</button>
                </div>
            </div>
            <div class="from-body withheader" style=" overflow: auto;">
                <iframe id="iFrame"></iframe>
            </div>
        </div>
    </div>
    <!-- whitebox -->
</div>
<script type="text/javascript">
    var vm;
    var zTreeObj;
    avalon.ready(function () {
        vm = avalon.define({
            $id: "main",
            treeDatas: [],//字典数据
            trainTypes: [],//所有车型集合
            trainCode: "",//当前选择的车型
            title: '工序管理',
            selectNodeId: '',//选中的节点ID
            selectNodeName: '',//选中节点的名字
            parentNodeId: '',//选中节点的父节点id
            parentNodeName: ''//父节点名称
        })
        avalon.scan();
        loadTreeData();
        loadSearchData();
    });

    function selectClick(_this) {
        $(_this).trigger("click");
        console.log("asdasd");
    }

    //当select值改变的时候触发
    function selectChange(_this) {
        var selectValue = $(_this).val();
        vm.trainCode = selectValue;
        console.log(selectValue);
        loadTreeData();
    }

    //加载车型数据
    function loadSearchData() {
        $.ajax({
            type: 'post',
            url: '${ctx}/codeController/selectAllCodeData.action?sysCode=cxgl',
            data: "",
            dataType: "json",
            timeout: 10000,
            success: function (data) {
                vm.trainTypes = data.data;
            }
        });

    }

    //删除节点
    function deleteSelectNode() {
        var nodes = zTreeObj.getSelectedNodes();
        if (nodes.length == 0) {
            layerUtil.fail("你没有选择节点，请先选择节点");
            return;
        }
        layerUtil.confirm("你确定删除该节点吗，如果该节点包含子节点会将子节点一并删除,请慎重操作。", function () {
            ajaxUtil.post('${ctx}/workController/deleteWork.action?id=' + nodes[0].id, "", true, function (data) {
                layerUtil.success("删除成功");
                loadTreeData();
            })
        });
    }

    //加载菜单数据
    function loadTreeData() {
        var dataStr = {trainType: vm.$model.trainCode}
        ajaxUtil.post("${ctx}/workController/selectAllWork.action", dataStr, true, function (data) {
            vm.treeDatas = data.datas;
            initZTree();
        });
    }

    //初始化树
    function initZTree() {
        var setting = {
            data: {
                simpleData: {
                    enable: true, //是否启用简单数据类型
                    idKey: "id",
                    pIdKey: "parentId",
                    rootPId: 0
                }
            },
            callback: { //回调
                onClick: nodeOnClick
            }
        };
        zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, vm.$model.treeDatas);
        var nodes = zTreeObj.getNodes();
        zTreeObj.expandNode(nodes[0], true, false, false);
    }

    //新增工序
    function addWork() {
        var nodes = zTreeObj.getSelectedNodes();
        if (nodes.length == 0) {
            layerUtil.fail("你没有选择任何节点，请先选择节点");
            return;
        }
        vm.title = '新增工序';
        var url = "${ctx}/workController/toWorkFormSave.action?parentId=" + vm.$model.selectNodeId;
        setIframeSrc(url);
    }

    //点击节点的回调
    function nodeOnClick(event, treeId, treeNode) {
        vm.selectNodeId = treeNode.id;
        vm.selectNodeName = treeNode.name;
        if (treeNode.id != '0') {
            vm.title = "编辑工序";
            var parentNode = treeNode.getParentNode();
            vm.parentNodeId = parentNode.id;
            vm.parentNodeName = parentNode.name;
            var url = '${ctx}/workController/toWorkFormUpdate.action?id=' + treeNode.id + "&trainType=" + vm.$model.trainCode;
            setIframeSrc(url);
        }
    }

    //上移
    function move(type) {
        var nodes = zTreeObj.getSelectedNodes();
        if (nodes.length == 0 || nodes[0].id == '0') {
            layerUtil.fail("你没有选择任何节点，请先选择节点");
            return;
        }
        var node = nodes[0];
        var preNode = type == 1 ? node.getPreNode() : node.getNextNode();
        if (preNode == null) {
            layerUtil.fail(type == 1 ? "该节点已经是第一个了" : "该节点已经是最后一个了");
            return;
        }
        var dataStr = {id1: node.id, id2: preNode.id};
        ajaxUtil.post("${ctx}/workController/moveWork.action", dataStr, true, function (data) {
            zTreeObj.moveNode(preNode, node, type == 1 ? "prev" : "next");
        })
    }

    //设置iframe url地址
    function setIframeSrc(url) {
        $("#iFrame").attr("src", url);
    }
</script>
</body>
</html>